// pmView.cpp : implementation of the CPmView class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include <atlmisc.h>
#include <atlgdi.h>

#include "ListView.h"
#include "Utils.h"

void ListView::SetModel(ListModel * mdl)
{
    model = mdl;
    SetItemCountEx(model->Count(), LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL);
}

std::wstring ListView::GetItemText(int item, int subitem)
{
    std::vector<wchar_t> buff;
    buff.assign(4 + model->GetItem(item).Title.size() + 1, 0);

    swprintf(&buff[0], L"%2d. %s", item + 1, model->GetItem(item).Title.c_str());

    return &buff[0];
}

static std::wstring noItemsStr = utils::LoadString(IDR_LBL_NOMEMOS);

LRESULT ListView::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
    if (model && !model->Count()) {
        CPaintDC dc(m_hWnd);

        CRect rect;
        GetClientRect(&rect);
        rect.OffsetRect(CPoint(0, 10));

        dc.SetTextColor(GetSysColor(COLOR_GRAYTEXT));
        dc.DrawText(noItemsStr.c_str(), noItemsStr.size(), &rect, DT_CENTER | DT_WORDBREAK);
    }

    return bHandled = FALSE;
}

void ListView::UpdateModel()
{
    SetItemCountEx(model->Count(), LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL);
    UpdateItemWidth();

    Invalidate();
}

void ListView::UpdateItemWidth()
{
    CRect rect;
    GetClientRect(&rect);
    SetColumnWidth(0, rect.Width());
}